package com.inspur.zhangpeixia.goods.dao;

import com.inspur.zhangpeixia.goods.entity.Category;
import com.inspur.zhangpeixia.goods.entity.Good;
import com.inspur.zhangpeixia.goods.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GoodDao {
    private Connection connection = DBUtils.getConnection();

    public List<Good> listGoods(String kw) {
        try {
            List<Good> goods = new ArrayList<>();
            String sql = "select g.goods_id, g.category_id, c.category_name, g.goods_name,g.goods_price, g.goods_stock " +
                    "from t_goods g join t_category c on g.category_id = c.category_id where g.goods_name like ?";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, "%" + kw + "%");
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next()) {
                Good good = new Good();
                good.setGoodsId(resultSet.getInt("goods_id"));
                good.setCategoryId(resultSet.getInt("category_id"));
                good.setGoodsName(resultSet.getString("goods_name"));
                good.setCategoryName(resultSet.getString("category_name"));
                good.setGoodsPrice(resultSet.getDouble("goods_price"));
                good.setGoodsStock(resultSet.getInt("goods_stock"));
                goods.add(good);

            }
            return goods;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public int addGood(Good good) {
        try {
            String sql = "insert into t_goods (category_id, goods_name, goods_price, goods_stock) values (?, ?, ?, ?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setInt(1, good.getCategoryId());
            pst.setString(2, good.getGoodsName());
            pst.setDouble(3, good.getGoodsPrice());
            pst.setInt(4, good.getGoodsStock());
            return pst.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public int updateGood(Good good) {
        try {
            String sql = "update t_goods set goods_name=?, goods_price=?, goods_stock=? where goods_id=?";
            PreparedStatement pst = connection.prepareStatement(sql);
         //   pst.setInt(1, good.getCategoryId());
            pst.setString(1, good.getGoodsName());
            pst.setDouble(2, good.getGoodsPrice());
            pst.setInt(3, good.getGoodsStock());
            pst.setInt(4, good.getGoodsId());
            return pst.executeUpdate();
        } catch (Exception e) {
            // 这里别直接抛 RuntimeException，打日志方便排查
            throw new RuntimeException(e);
        }
    }
    public int deleteGood(int id) {
        try {
            String sql = "delete from t_goods where goods_id=?";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setInt(1, id);
            return pst.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}